package net.alan.modules;

import net.alan.common.utils.MD5Util;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/23
 */
public class Demo {

    public static void main(String[] args) {
        System.out.println(MD5Util.md5("123456"));
    }

    @Test
    public void sort() {
        List<String> names1 = new ArrayList<String>();
        names1.add("Google ");
        names1.add("Runoob ");
        names1.add("Taobao ");
        names1.add("Baidu ");
        names1.add("Sina ");
        System.out.println(names1);
        Collections.sort(names1, (s1, s2) -> s1.compareTo(s2));
        System.out.println(names1);

        names1.forEach(System.out::println);
    }

    @Test
    public void math() {
        // 类型声明
        MathOperation addition = (int a, int b) -> a + b;
        // 不用类型声明
        MathOperation subtraction = (a, b) -> a - b;
        // 大括号中的返回语句
        MathOperation multiplication = (int a, int b) -> {
            return a * b;
        };

        System.out.println(multiplication.operation(12, 41));

        // 不用括号
        GreetingService greetService1 = message ->
                System.out.println("Hello " + message);

        // 用括号
        GreetingService greetService2 = (message) ->
                System.out.println("Hello " + message);

        greetService1.sayMessage("Runoob");
        greetService2.sayMessage("Google");
    }

    public interface MathOperation {
        int operation(int a, int b);
    }

    public interface GreetingService {
        void sayMessage(String message);
    }

    @FunctionalInterface
    public interface Supplier<T> {
        T get();
    }

    @FunctionalInterface
    public interface Act<Integer> {
        boolean add(int a, int b);
    }

    static class Car {
        //Supplier是jdk1.8的接口，这里和lamda一起使用了
        public static Car create(final Supplier<Car> supplier) {
            return supplier.get();
        }

        public static void collide(final Car car) {
            System.out.println("Collided " + car.toString());
        }

        public void follow(final Car another) {
            System.out.println("Following the " + another.toString());
        }

        public void repair() {
            System.out.println("Repaired " + this.toString());
        }
    }

    @Test
    public void cla() {
        final Car car = Car.create(Car::new);
        final List<Car> cars = Arrays.asList(car);
        cars.forEach(Car::collide);

        cars.forEach(Car::repair);

        final Car police = Car.create(Car::new);
        cars.forEach(police::follow);

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        deal(list, (x, y) -> (x < y));

        Random random = new Random();
        random.ints(10, 1000).limit(10).forEach(System.out::println);

        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
        List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
    }

    private void deal(List<Integer> list, Act<Integer> act) {
        for (Integer n : list) {
            if (act.add(n, n * 2)) {
                System.out.println(n + " ");
            }
        }
    }

}
